package com.sne.common.entity;

import com.sne.common.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * 返回实体
 *
 * @author fanghh
 * @since 2019/12/30.
 */
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    private int code;
    private String msg = "";
    private T data;

    public static Result success() {
        return restResult(ResultCode.SUCCESS, null);
    }

    public static <T> Result success(T data) {
        return restResult(ResultCode.SUCCESS, data);
    }

    public static <T> Result success(String msg, T data) {
        return restResult(ResultCode.SUCCESS.getCode(), msg, data);
    }

    public static Result failed() {
        return restResult(ResultCode.FAIL, null);
    }

    public static <T> Result failed(T data) {
        return restResult(ResultCode.FAIL, data);
    }

    public static <T> Result failed(String msg, T data) {
        return restResult(ResultCode.FAIL.getCode(), msg, data);
    }

    private static <T> Result restResult(int code, String msg, T data) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        return apiResult;
    }

    public static <T> Result restResult(ResultCode resultEnum, T data) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(resultEnum.getCode());
        apiResult.setMsg(resultEnum.getMsg());
        apiResult.setData(data);
        return apiResult;
    }

    public static <T> Result restResult(int code,String msg){
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        return apiResult;
    }


}
